
#import "ViewController.h"
#import "GCHPrivacyAgreementView.h"
#import "AppDelegate.h"

@interface ViewController ()<GCHPrivacyAgreementDelegate,UIApplicationDelegate>
@property(nonatomic)int isFirst;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *imageView=[[UIImageView alloc]init];
    imageView.frame=CGRectMake(0, 0, _window_width, _window_height);
    imageView.contentMode=UIViewContentModeScaleToFill;
    imageView.image=[UIImage imageNamed:@"launch_image"];
    [self.view addSubview:imageView];
    
   
//    CPPrivacyAgreementView *view =[[CPPrivacyAgreementView alloc]initCPTipViewWithContent:@"您当前选择的性别为：女 \n确认后不可更改，是否确认注册？" title:@"" confirmStr:@"确定注册" cancelStr:@"返回修改" buttonCount:2];
//    view.delegate=self;
//    [self.view addSubview:view];
    
//    [[UIApplication sharedApplication].delegate.window addSubview:view];
//    [view show];
    
    NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isFirstInstall = [UserDefaults objectForKey:@"isFirstInstall"];
    if ([isFirstInstall isEqualToString:@""]||isFirstInstall==NULL||isFirstInstall==nil) {//第一次安装app,没同意隐私协议
        NSLog(@"第一次安装app");
     
        GCHPrivacyAgreementView *view=[[GCHPrivacyAgreementView alloc]initWithFrame:self.view.frame];
        view.PAdelegate=self;
        [view showPrivacyAgreementView:self.view];
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:@"Y" forKey:newsVoice([Config getID])];
        [userDefaults setObject:@"Y" forKey:newsShock([Config getID])];
        [userDefaults setObject:@"Y" forKey:callTHVoice([Config getID])];
        [userDefaults setObject:@"Y" forKey:receiveTHVoice([Config getID])];
        [userDefaults setObject:@"Y" forKey:newsView([Config getID])];
        
        
        [userDefaults synchronize];
    }else{
//        if ([Config getToken]==nil) {
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                FLPhoneLogin *vc=[[FLPhoneLogin alloc]init];
//                vc.isFirst=1;
//                UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
//                navVc.modalPresentationStyle=0;
//                [self presentViewController:navVc animated:NO completion:nil];
//            });
//        }
    }
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
       [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
           /*
        status
        AFNetworkReachabilityStatusUnknown          = -1, 不知道监测的是什么
        AFNetworkReachabilityStatusNotReachable     = 0,  没有检测到网络
        AFNetworkReachabilityStatusReachableViaWWAN = 1,  蜂窝网
        AFNetworkReachabilityStatusReachableViaWiFi = 2,  WIFI
        */
           switch (status) {
                   //没有检测到网络
               case AFNetworkReachabilityStatusNotReachable:
//                   [alert show];
                   break;
               case AFNetworkReachabilityStatusReachableViaWWAN:
                   [self loadsystemopen_app_config];
                   break;
               case AFNetworkReachabilityStatusReachableViaWiFi:
                   [self loadsystemopen_app_config];
                   break;
               default:
                   break;
           }
       }];
       // 开始监测
       [manager startMonitoring];
                           
}


-(void)clickPrivacyAgreementIndex:(NSInteger)index{
    NSDictionary *saveOpen_app_config = [Config getOpenAppConfig];
    NSString *h5_url=@"";
    if (saveOpen_app_config!=nil) {
        h5_url=saveOpen_app_config[@"h5_url"];
    }
    
    PUWebViewVC *vc=[[PUWebViewVC alloc]init];
    NSString *url=@"";
    if (index==0) {//用户服务协议
        vc.titleStr=@"用户服务协议";
        url = [NSString stringWithFormat:@"%@%@?p=4",h5_url,@"user_agreement"];
    }
    if (index==1) {//隐私政策
        vc.titleStr=@"隐私政策";
        url = [NSString stringWithFormat:@"%@%@?p=4",h5_url,@"privacy_policy"];
    }
    if (index==2) {//个人信息收集清单
        url=@"";
        vc.titleStr=@"个人信息收集清单";
        url = [NSString stringWithFormat:@"%@%@?p=4",h5_url,@"personal_collect_inventory"];
    }
    if (index==3) {//第三方共享个人信息清单
        
        vc.titleStr=@"第三方共享个人信息清单";
        url = [NSString stringWithFormat:@"%@%@?p=4",h5_url,@"sdk_collect_inventory"];
    }
    vc.is_topbar=0;
    vc.isXYEnter=1;
    vc.isfirstEnter=1;
    vc.url=url;
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    navVc.modalPresentationStyle=0;
    [self presentViewController:navVc animated:NO completion:nil];
    
}

-(void)clickButton:(NSInteger)buttonIndex withView:(GCHPrivacyAgreementView*)privacyAgreementView{
    if(buttonIndex==101){//同意
       NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
       [userDefaults setObject:@"0" forKey:@"isFirstInstall"];
       [userDefaults synchronize];
                
        AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
        [appDelegate didFinishLaunchingWithOptions];

//        FLPhoneLogin *vc=[[FLPhoneLogin alloc]init];
//        vc.isFirst=1;
//        UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
//        navVc.modalPresentationStyle=0;
//        [self presentViewController:navVc animated:NO completion:nil];

    }else{//不同意
        [[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];
    }
}

-(void)CPPrivacyAgreementViewBtnClickedIndex:(int)index{
    if(index==0){
        NSLog(@"不同意");
    }else{
        NSLog(@"同意");
    }
}


-(void)loadsystemopen_app_config{
    
    NSString *path=@"/system/open_app_config";
    NSString *url = [NSString stringWithFormat:@"%@%@",baseUrl,path];
    [[AFNetManager shareManager] postDataWithURLString:url path:path parameters:@{} success:^(NSURLResponse *operation, id successObject) {
        NSLog(@"%@",successObject);
        
        NSString *result=successObject[@"result"];
        NSDictionary *dic=@{};
        if ([successObject[@"result"] isKindOfClass:[NSString class]]) {
            //解密Base64编码的字符串
            NSString *decryptedString = [AES128 decryptString:result key:dataKey iv:dataIv];
            dic=[AES128 dictionaryWithJsonString:decryptedString];
            NSLog(@"json解析===%@",dic);
        }
        
        NSDictionary *saveOpen_app_config=dic;
        [Config saveOpen_app_config:saveOpen_app_config];
        NSDictionary *saveOpen_app_config1 = [Config getOpenAppConfig];
        NSString *tim_app_id=@"";
        NSString *cos_bucket_name=@"";
        NSString *cos_region=@"";
         
        if (saveOpen_app_config1!=nil) {
            tim_app_id=saveOpen_app_config1[@"tim_app_id"];
            NSLog(@"tim_app_id=%@",tim_app_id);
            cos_bucket_name=saveOpen_app_config1[@"cos_bucket_name"];
            NSLog(@"cos_bucket_name=%@",cos_bucket_name);
            cos_region=saveOpen_app_config1[@"cos_region"];
            NSLog(@"cos_region=%@",cos_region);
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

@end

